package com.orhon.smartcampus.modules.systemctl.mutation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.systemctl.dao.OperationDepartmentRelationDao;
import com.orhon.smartcampus.modules.systemctl.dao.OperationsDao;
import com.orhon.smartcampus.modules.systemctl.dao.OrgDepartmentsDao;
import com.orhon.smartcampus.modules.systemctl.dao.OrgDutiesDao;
import com.orhon.smartcampus.modules.systemctl.entity.OperationDepartmentRelation;
import com.orhon.smartcampus.modules.systemctl.entity.Operations;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDepartments;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDuties;
import com.orhon.smartcampus.modules.systemctl.gqlInputModel.GQLOperationDepartmentRelationInput;
import com.orhon.smartcampus.modules.systemctl.inputMapper.OperationDepartmentRelationInputMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddDepartmentDutyOperationMutation implements DataFetcher {

    @Autowired
    OperationDepartmentRelationDao operationDepartmentRelationDao;

    @Autowired
    OperationsDao operationsDao;

    @Autowired
    OrgDutiesDao orgDutiesDao;

    @Autowired
    OrgDepartmentsDao orgDepartmentsDao;

    /**
     * 给部门职务分配权限
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        GQLOperationDepartmentRelationInput input = OperationDepartmentRelationInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        OperationDepartmentRelation operationDepartmentRelation = null;

        operationDepartmentRelation = new OperationDepartmentRelation();

        if(input.getOperation_id() != null) {
            operationDepartmentRelation.setSystemctlOperationsByOperationId(operationsDao.get(Operations.class, input.getOperation_id()));
        }
        if(input.getDuty_id() != null) {
            operationDepartmentRelation.setSystemctlOrgDutiesByParentId(orgDutiesDao.get(OrgDuties.class, input.getDuty_id()));
        }
        if(input.getDepartment_id() != null) {
            operationDepartmentRelation.setSystemctlOrgDepartmentsByDepartmentId(orgDepartmentsDao.get(OrgDepartments.class, input.getDepartment_id()));
        }
        try{
            operationDepartmentRelationDao.saveOrUpdate(operationDepartmentRelation);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

